﻿using System;
using TallComponents.PDF.Rasterizer.Configuration;
using TallComponents.Web.Pdf.Annotations;
using System.Collections.Generic;
using TallComponents.PDF.Rasterizer.Diagnostics;
using TallComponents.PDF.TextExtraction;

namespace TallComponents.Web.Pdf
{
   /// <summary>
   /// Provides information about a page in a PDF document.
   /// </summary>
   /// <seealso cref="Document"/>
   public sealed class Page
   {
      /// <summary>
      /// Initializes a new instance of the <see cref="Page"/> class.
      /// </summary>
      /// <param name="document">The document.</param>
      /// <param name="index">The index.</param>
      /// <param name="width">The width.</param>
      /// <param name="height">The height.</param>
      /// <param name="links">The collection of links for this page.</param>
      /// <exception cref="ArgumentNullException">Occures when the <paramref name="document"/> is <c>null</c>.</exception>
      internal Page( Document document, int index, double width, double height, System.Collections.Generic.ICollection<Link> links )
      {
         if ( null == document )
            throw new ArgumentNullException( "document" );

         if ( index < 0 )
            throw new ArgumentOutOfRangeException( "index" );

         _document = document;
         _index = index;
         Width = width;
         Height = height;
         Links = links;
      }

      /// <summary>
      /// Gets the width of the page in points (1/72 inch).
      /// </summary>
      /// <value>The width of the page in points (1/72 inch).</value>
      public double Width { get; private set; }

      /// <summary>
      /// Gets the height of the page in points (1/72 inch).
      /// </summary>
      /// <value>The height of the page in points (1/72 inch).</value>
      public double Height { get; private set; }

      /// <summary>
      /// Gets the annotations for this page.
      /// </summary>
      /// <value>The annotations.</value>
      public System.Collections.Generic.ICollection<Link> Links { get; private set; }

      /// <summary>
      /// Draws the page content.
      /// </summary>
      /// <param name="graphics">The graphics object to which the content is drawn.</param>
      /// <param name="renderSettings">Settings that control the render process.</param>
      /// <param name="summary">The summary.</param>
      internal void Draw(System.Drawing.Graphics graphics, RenderSettings renderSettings, Summary summary)
      {
         _document.DrawPage( _index, graphics, renderSettings, summary );
      }

      /// <summary>
      /// Starts a search for the specified text on the specified page.
      /// </summary>
      /// <param name="text">The text to look for.</param>
      /// <param name="matchCase">if set to <c>true</c> the search is case sensitive.</param>
      /// <param name="matchWholeWord">if set to <c>true</c> only whole word matches are returned.</param>
      /// <returns>
      /// All occurences of the specified text in the document.
      /// </returns>
      /// <remarks>Searching can take some time so the text matches are returend on demand. Every time the iterator receives a call
      /// to MoveNext (for example the next iteration in a foreach loop) a search is started for the next occurrence.</remarks>
      internal IEnumerable<TextMatch> Find( string text, bool matchCase, bool matchWholeWord )
      {
         return _document.Find( _index, text, matchCase, matchWholeWord );
      }

      private readonly int _index;
      private readonly Document _document;
   }
}
